package com.lamer.console.blog.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lamer.console.blog.entity.Book;
import com.lamer.console.blog.service.IBookService;
import com.lamer.console.blog.vo.BookVO;
import com.lamer.console.blog.vo.ChapterVO;
import com.lamer.console.common.api.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黄诗鹏
 * @since 2021-11-24
 */
@RestController
@RequestMapping("/blog")
public class BookController {

    @Autowired
    private IBookService bookService;

    @GetMapping("/book/page/")
    public ApiResult<Page<BookVO>> getPage(@RequestParam("search") String search) {
        JSONObject jsonObject = JSON.parseObject(search);
        Integer number = jsonObject.getInteger("page_number");
        Integer size = jsonObject.getInteger("page_size");
        String bookName = jsonObject.getString("bookName");
        JSONArray authorIdArray = jsonObject.getJSONArray("authorId");
        JSONArray categoryIdArray = jsonObject.getJSONArray("categoryId");

        QueryWrapper<BookVO> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(bookName)) queryWrapper.like("book.book_name", bookName);
        if(!authorIdArray.isEmpty()) queryWrapper.in("book.author_id", authorIdArray);
        if(!categoryIdArray.isEmpty()) queryWrapper.in("book.category_id", categoryIdArray);

        Page<BookVO> page = bookService.pageBookVO(new Page<>(number, size), queryWrapper);
        return ApiResult.success(page);
    }

    @GetMapping("/book/{id}")
    public ApiResult<Book> getData(@PathVariable("id") int id) {
        Book data = bookService.getById(id);
        return data == null ? ApiResult.failed() : ApiResult.success(data);
    }

    @PostMapping("/book/")
    public ApiResult<String> insertData(@RequestBody Book data) {
        boolean flag = bookService.save(data);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @PutMapping("/book/")
    public ApiResult<String> updateData(@RequestBody Book data) {
        boolean flag = bookService.updateById(data);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @DeleteMapping("/book/list/")
    public ApiResult<String> deleteData(@RequestParam("ids") int[] ids) {
        List<Integer> collect = Arrays.stream(ids).boxed().collect(Collectors.toList());
        boolean flag = bookService.removeByIds(collect);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @GetMapping("/book/list/")
    public ApiResult<List<Book>> getBookList() {
        List<Book> list = bookService.list();
        return ApiResult.success(list);
    }

}

